<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		

		<title>My JSP 'usergrouptaskallocation.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/common.css" type="text/css" />

		<s:head />
		
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/N13FormValidation-2.js"></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/dWRSystemUserAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>
		<%
		
			if( null == session.getAttribute("systemusergroupvalue") ||  null == session.getAttribute("systemusergroupid") ){
				out.println("<script type='text/javascript'>");
				out.println("alert('您尚未登录,无法查看任务!');");
				out.println("</script>");
				return;
			}
		
		%>	
		<script type="text/javascript">
			//查询所有的用户组
			function getAllGroup(value){
				dWRSystemUserAction.findAllGroup(value,findAllGroupCallback);
			}
			var findAllGroupCallback = function findAllGroupCallbackFunc(groupList){
				  if(null == groupList){
				  	return;
				  }
				  var groupInfo =groupList;
					var s = $("systemusergroupid");
					 	DWRUtil.removeAllOptions(s);  
						s.options[0] = new Option("请选择用户组",'0');
					for ( var i = 1; i < groupList.length+1; i++) {
						var opt = new Option(groupList[i-1].systemusergroupname,
								groupList[i-1].systemusergroupid);
						s.options[i] = opt;
					}
			}
			
			//找到所有用户组权限值低于自己的用户.如果这里的用户很多,将影响性能,后期可能考虑去除此功能
			function getAllUserBebindValue(systemusergroupid,systemusergroupvalue){
				dWRSystemUserAction.findAllUserBehindValue(systemusergroupid,systemusergroupvalue,findAllUserBehindValueCallback);
			}
			var findAllUserBehindValueCallback = function findAllUserBehindValueCallbackFunc(systemUserList){
			  		var userInfo =systemUserList;
					var s = $("systemusertaskaccepter");
					DWRUtil.removeAllOptions(s);  
						s.options[0] = new Option("请选择用户",'0');
					for ( var i = 1; i < systemUserList.length+1; i++) {
						var opt = new Option(systemUserList[i-1].systemusername,
								systemUserList[i-1].systemusergroupid+"_"+systemUserList[i-1].systemuserid);
						s.options[i] = opt;
					}
			}
			//选择用户时,用户组联动
			function getGroupByUser(obj){
				var s =  obj.options[obj.selectedIndex].value
				for(var a=0;a<${'systemusergroupid'}.options.length;a++){
					if(${'systemusergroupid'}.options[a].value == s.substr(0,s.indexOf("_"))){
						${'systemusergroupid'}.options[a].selected = "selected";
					};
				}
			
			}
			//选择用户组时,用户联动
			function getUserByGroup(obj){
				dWRSystemUserAction.findUserByGroup(obj.options[obj.selectedIndex].value,findUserByGroupCallback);
				
			}
			var findUserByGroupCallback = function findUserByGroupCallbackFunc(systemUserList){
				if(null == systemUserList){
					DWRUtil.removeAllOptions($("systemusertaskaccepter"));  
					$("systemusertaskaccepter").options[0] = new Option("请选择用户",'0');
					return;
				}
				var userInfo =systemUserList;
				var s = $("systemusertaskaccepter");
				DWRUtil.removeAllOptions(s);  
					s.options[0] = new Option("请选择用户",'0');
				for ( var i = 1; i < systemUserList.length+1; i++) {
					var opt = new Option(systemUserList[i-1].systemusername,
							systemUserList[i-1].systemusergroupid+"_"+systemUserList[i-1].systemuserid);
					s.options[i] = opt;
				}
			}
			function submitForm(){
				if($('systemusergroupid').options[$('systemusergroupid').selectedIndex].value == '0'){
					alert("请选择任务接受者");
					return;
				}
				var  sa = $('systemusertaskaccepter').options[$('systemusertaskaccepter').selectedIndex];
				$('accepterid').value = sa.value.substr(sa.value.indexOf("_")+1);
				$('accepter').value = sa.text;
				//alert($('accepter').value);
				//alert($('accepterid').value);
				$('systemusertaskacceptergroupname').value = $('systemusergroupid').options[$('systemusergroupid').selectedIndex].innerHTML;
				//alert($('systemusertaskacceptergroupname').value);
				$('taskForm').submit();
			}
			//
				
		</script>
	</head>
	
	<body>
		<div id="page_container">
			<div id="page_nav">
			<%@   include   file="banner.jsp"   %> 
			</div>
			<h2>分配新任务</h2>
			<s:form action="allocateTask.action"
				method="post" theme="simple" id="taskForm">
				<table class="table_common" width="600px">
					<tr>
						<td class="td_left">
							任务名称：
						</td>
						<td class="td_right">
							<s:textfield name='systemusertask.systemusertasktitle'
								id='systemusertasktitle' />
						</td>
					</tr>
					<tr>
						<td class="td_left">
							任务接受者:
						</td>
						<td class="td_right">
							<select class="common_select" name='systemusertask.taskacceptergroupid' style="width:105px"
								id='systemusergroupid' onchange="getUserByGroup(this)">
								
							</select>
							<input type="hidden" name='systemusertask.systemusertaskaccepter'
								id='accepter' />
							<input type="hidden"
								name='systemusertask.systemusertaskaccepterid' id='accepterid' />
							<input type="hidden" name='systemusertask.systemusertaskacceptergroupname'
								id='systemusertaskacceptergroupname' />
							<select class="common_select" name='accepterid' id='systemusertaskaccepter' style="width:100px"
								onchange="getGroupByUser(this)">
								
							</select>
						</td>
						<td>
					</tr>
					<tr>
						<td class="td_left">
							任务到期时间：
						</td>
						<td class="td_right">
							<s:datetimepicker name='systemusertask.systemusertaskduetime'
								id='systemusertaskduetime'
								label="Format (yyyy-MM-dd)" displayFormat="yyyy-MM-dd" />

						</td>
					</tr>
					<tr>
						<td class="td_left">
							任务内容：
						</td>
						<td class="td_right">
							<s:textarea name='systemusertask.systemusetaskcontent' cols="40"
								rows="6" id='systemusetaskcontent' />

						</td>
					</tr>


					<tr>
						<td class="td_left">
							任务备注：
						</td>
						<td class="td_right">
							<s:textarea name='systemusertask.systemusertaskmisc' cols="40"
								rows="3" id='systemusertaskmisc' />
						</td>
					<tr class="table_submit">
						<td colspan="2">
							<input class="common_button"  type="button" value="提交新任务" onclick="submitForm()">
						</td>
					</tr>
				</table>
			</s:form>
		</div>
		<script type="text/javascript">

			N13.register('taskForm',[
					{
						id:'systemusertasktitle',
						type:'Required',
						validMsg:'√',
						invalidMsg:'请不要为空',
						validOnBlur:true,
						renderValid:true
					}
				]);
			window.onload = function(){
				
				getAllGroup(<%=session.getAttribute("systemusergroupvalue") %>);
				getAllUserBebindValue(<%=session.getAttribute("systemusergroupid") %>
					,<%=session.getAttribute("systemusergroupvalue") %>);
				
				
				var nav_a=$j('#page_nav a');
				var nav_span=$j("#page_nav a span");
				for(var u = 0 ;u<nav_a.length;u++){
						nav_a.get(u).style.backgroundPosition="left top";
						nav_span.get(u).style.backgroundPosition="right top";							
				}
				$j('#page_nav a').get(0).style.backgroundPosition="0% -42px";
				$j("#page_nav a span").get(0).style.backgroundPosition="100% -42px";
			}	
				
			</script>
	</body>
</html>
